<template>
  <van-picker
    title="选择性别"
    show-toolbar
    :columns="columns"
    :default-index="value"
    @cancel="$emit('close')"
    @confirm="onConfirm"
  />
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: '',
  components: {},
  props: {
    // 接收用户性别
    value: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      columns: ['男', '女'],
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    //处理点击操作
    async onConfirm(value, index) {
      console.log(index)
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true, // 禁止背景点击
        duration: 0, // 持续展示
      })

      try {
        await updateUserProfile({
          gender: index,
        })

        // 更新视图
        this.$emit('input', index)

        // 关闭弹层
        this.$emit('close')

        // 提示成功
        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('更新失败')
      }
    },
  },
}
</script>

<style lang="less" scoped></style>
